<?php
	include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/inc/usedb_inc.php");
	class DAL_Authenticate
	{
		private static $dbContext;
		
		public static function IsExist($email)
		{
			if(empty($email))
			{
				return false;
			}
			return DAL_Authenticate::SelectUser($email) != null;
		}  
		
		public static function CreateUser($email, $password, $verification_code, $user_salt)
		{
			$is_verified = true;
			return DAL_Authenticate::CreateUser2($email, $password, '', '', $is_verified, true, false, $verification_code, $user_salt);
		}
		
		//todo: 蛋疼的php方法重载机制, 暂时先用命名搞定吧
		public static function CreateUser2($email, $password, $username, $address, $is_verified, $is_active, $is_admin, $verification_code, $user_salt)
		{
			//先进行重名检测
			$select = DAL_Authenticate::IsExist($email);
			if($select == true)
				return false; //todo: 这个false会跟本身的操作失败混淆, 需要区分开来
			
			//todo: 需要返回新建user的标识. 或者返回整个user对象也行
			$dbContext = new SqliteHelper();
			$parameters = array(":email" => $email, 
								":password" => $password,
								":username" => $username,
								":address" => $address,
								":is_verified" => $is_verified,
								":is_active" => $is_active,
								":is_admin" => $is_admin,
								":user_salt" => $user_salt,
								":verification_code" => $verification_code);
			$sqlStr = "insert into [user] (email, password, username, address, is_verified, is_active, is_admin, user_salt, verification_code)" . 
			"values (:email, :password, :username, :address, :is_verified, :is_active,:is_admin, :user_salt, :verification_code)";
			
			return $dbContext->ExecNoTans($sqlStr, $parameters);
		}
		
		public static function SelectUserVisitLog($userid)
		{
			$dbContext = new SqliteHelper();
			$parameters = array(":user_id" => $userid);
			$sqlStr = "select * from [logged_in_member] where logged_in_member.user_id=:user_id";
				
			return $dbContext->Query($sqlStr, $parameters);
		}
		
		public static function UpdateUserVisitLog($userid, $newsessionid, $newusertoken)
		{
			$dbContext = new SqliteHelper();
			$parameters = array(":user_id" => $userid, ":session_id" => $newsessionid, ":token" => $newusertoken);
			$sqlStr = "update logged_in_member set session_id =:session_id, token =:token where user_id =:user_id";
		
			return $dbContext->ExecNoTans($sqlStr, $parameters);
		}
		
		public static function InsertUserVisitLog($sessionid, $userid, $token)
		{
			$dbContext = new SqliteHelper();
			$parameters = array(":session_id" => $sessionid, ":user_id" => $userid, ":token" => $token);
			$sqlStr = "insert into [logged_in_member] (session_id, user_id, token) values (:session_id, :user_id, :token)";
			
			return $dbContext->ExecNoTans($sqlStr, $parameters);
		}
		
		public static function DeleteUserVisitLog($userid, $sessionid)
		{
			$dbContext = new SqliteHelper();
			$parameters = array(":session_id" => $sessionid, ":user_id" => $userid);
			$sqlStr = "delete from [logged_in_member] where logged_in_member.session_id =:session_id and logged_in_member.user_id =:user_id";
			
			return $dbContext->ExecNoTans($sqlStr, $parameters);
		}
		
		public static function SelectUser($email) 
		{
			$dbContext = new SqliteHelper();
			$parameters = array(":email" => $email);
			$sqlStr = "select * from [user] where user.email=:email";
			
			return $dbContext->Query($sqlStr, $parameters);
		}
		
	}
?>